﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenerateDbApp
{



    public class DBWriter
    {
        private static DBWriter Writer = null;

        private DBWriter() 
        {
        }
        public static DBWriter GetDBWritter()
        {
            if (Writer == null)
                Writer = new DBWriter();
            return Writer;
        }
        public void WriteToDay(DateTime dDate, string sCurrency, double dValue)
        {
            using (ExtRatesEntities context = new ExtRatesEntities())
            {
                int iCurrencyId = context.Currency.Where(c => c.Name == sCurrency).FirstOrDefault().ID;

                cache_Day Item = new cache_Day()
                {
                    Period = dDate,
                    CurrencyID = iCurrencyId,
                    Value = dValue
                };
                context.AddTocache_Day(Item);
                context.SaveChanges();
            }
        }
        public void WriteToWeek(DateTime dDate, string sCurrency, double dValue)
        {
            using (ExtRatesEntities context = new ExtRatesEntities())
            {
                int iCurrencyId = context.Currency.Where(c => c.Name == sCurrency).FirstOrDefault().ID;

                cache_Week Item = new cache_Week()
                {
                    Period = dDate,
                    CurrencyID = iCurrencyId,
                    Value = dValue
                };
                context.AddTocache_Week(Item);
                context.SaveChanges();
            }
        }
        public void WriteToMonth(DateTime dDate, string sCurrency, double dValue)
        {
            using (ExtRatesEntities context = new ExtRatesEntities())
            {
                int iCurrencyId = context.Currency.Where(c => c.Name == sCurrency).FirstOrDefault().ID;

                cache_Month Item = new cache_Month()
                {
                    Period = dDate,
                    CurrencyID = iCurrencyId,
                    Value = dValue
                };
                context.AddTocache_Month(Item);
                context.SaveChanges();
            }
        }
        public void WriteToYear(DateTime dDate, string sCurrency, double dValue)
        {
            using (ExtRatesEntities context = new ExtRatesEntities())
            {
                int iCurrencyId = context.Currency.Where(c => c.Name == sCurrency).FirstOrDefault().ID;

                cache_Year Item = new cache_Year()
                {
                    Period = dDate,
                    CurrencyID = iCurrencyId,
                    Value = dValue
                };
                context.AddTocache_Year(Item);
                context.SaveChanges();
            }
        }
        public void WriteToPeriod(DateTime dDate)
        {
            using (ExtRatesEntities context = new ExtRatesEntities())
            {
                Period period = new Period()
                {
                    DateTime = dDate
                };
                context.AddToPeriod(period);
                context.SaveChanges();
            }
        }
        public void WriteToCurrency(string Value)
        {
            using (ExtRatesEntities context = new ExtRatesEntities())
            {
                Currency currency = new Currency()
                {
                    Name = Value
                };
                context.AddToCurrency(currency);
                context.SaveChanges();
            }
        }
        public void WriteToCurrencyRate(int iPeriodId, int iCurrencyId, double dValueValue)
        {
            using (ExtRatesEntities context = new ExtRatesEntities())
            {
                CurrencyRate currencyrate = new CurrencyRate()
                {
                    PeriodID = iPeriodId,
                    CurrencyID = iCurrencyId,
                    Value = dValueValue
                };
                context.AddToCurrencyRate(currencyrate);
                context.SaveChanges();
            }
        }
    }



}
